package data;

import util.ImportFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Abberation {
    private int lotteryNumber;
    private List<Passengers> abberationList;

    public Abberation(int lotteryNumber) {
        this.lotteryNumber = lotteryNumber;
    }

    public Abberation(int lotteryNumber, List<Passengers> abberationList) {
        this.lotteryNumber = lotteryNumber;
        this.abberationList = abberationList;
    }

    public int getLotteryNumber() {
        return lotteryNumber;
    }

    public void setLotteryNumber(int lotteryNumber) {
        this.lotteryNumber = lotteryNumber;
    }

    public List<Passengers> getAbberationList() {
        return abberationList;
    }

    public void setAbberationList(List<Passengers> abberationList) {
        this.abberationList = abberationList;
    }

    /*
     * 将文件取出来的值赋值
     * filePath:文件路径(一般来源于真实数据)
     * symbol:分割符
     * col_title:是否具有列名
     *
     * */
    public void assign(String filePath, String symbol, boolean col_title){
        //list:提取的数据（每一个元素是一行数据，未被分割）
        List list = ImportFile.readFile(filePath);

        int row_length = col_title?list.size()-1:list.size();
        int column_length = list.get(0).toString().split(symbol).length;
        String[] lineData = new String[column_length];

        List<Passengers> abberationList= new ArrayList<Passengers>();
        boolean flag;

        for (int i= 0 ;i<row_length;i++){
            if((col_title?list.get(0).toString().split(symbol):null)==null){
                lineData = list.get(i).toString().split(symbol);
            }else {
                lineData = list.get(i+1).toString().split(symbol);
            }
            flag = Integer.valueOf(lineData[4])>Constant.FLAG_THRESHOLD?true:false;
            abberationList.add(new Passengers(i,Integer.valueOf(lineData[4]),Double.valueOf(lineData[5]),flag));
        }
        this.abberationList =  abberationList;
    }

    //抽奖算法，来源于真实数据
    public static Passengers lottery(List<Passengers> abberationList) {
        //总的概率区间
        int totalPro = 0;
        //存储每个奖品新的概率区间
        List<Integer> proSection = new ArrayList<Integer>();
        proSection.add(0);
        //遍历每个奖品，设置概率区间，总的概率区间为每个概率区间的总和
        for (Passengers abbe : abberationList) {
            //每个概率区间为其畸变长度
            totalPro += abbe.getWidth();
            proSection.add(totalPro);
        }
        //获取总的概率区间中的随机数
        Random random = new Random();
        int randomPoint = random.nextInt(totalPro + 1); //需要保证为左闭右闭区间
        //判断取到的随机数在哪个奖品的概率区间中
        for (int i = 0, size = proSection.size(); i < size; i++) {
            if (randomPoint >= proSection.get(i)
                    && randomPoint < proSection.get(i + 1)) {
                return abberationList.get(i);
            }
        }
        return null;
    }
}
